package com.yy.glide.recycle;

import android.graphics.Bitmap;

import com.yy.glide.Key;

public class Resource {
    private Bitmap mBitmap;
    private int acquired;
    private ResourceListener mResourceListener;
    private Key mKey;

    public interface ResourceListener {
        void onResourceReleased(Key key, Resource resource);
    }

    public void setResourceListener(ResourceListener resourceListener, Key key) {
        mResourceListener = resourceListener;
        this.mKey = key;
    }

    public void recycle() {
        if (acquired > 0) {
            return;
        }
        if (!mBitmap.isRecycled()) {
            mBitmap.recycle();
        }
    }

    public void release() {
        if (--acquired == 0) {
            mResourceListener.onResourceReleased(mKey, this);
        }
    }

    public void acquire() {
        if (mBitmap.isRecycled()) {
            throw new IllegalStateException("Acquire a recycled resource");
        }
        ++acquired;
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.mBitmap = bitmap;
    }
}
